from collections import OrderedDict

from flask import Blueprint, request
from home import redisDB, db

from model import UserModel, UserResumeModel, UserAndJobModel, JobModel
from werkzeug.security import check_password_hash, generate_password_hash

bp = Blueprint("user", __name__, url_prefix="/user")

@bp.route("/userAccountCancellation",methods=["POST"])
def userAccountCancellation():
    msg = {
        "message": "",
        "type": "",
        "code": "",
    }
    # 检查身份
    identity = request.json["identity"]
    loginId = request.json["loginId"]

    if loginId:
        res = redisDB.handle_redis_token(loginId)
        if res:
            if identity == "user":
                from home.redisDB import delRedisOne
                userAndJobModel = UserAndJobModel.query.filter_by(uId=loginId).all()
                if userAndJobModel:
                    for i in userAndJobModel:
                        db.session.delete(i)
                        db.session.commit()
                resume=UserResumeModel.query.filter_by(uId=loginId).first()
                if resume:
                    db.session.delete(resume)
                    db.session.commit()
                user=UserModel.query.filter_by(uId=loginId).first()
                if user:
                    db.session.delete(user)
                    db.session.commit()
                delRedisOne(loginId)
                msg["message"] = "注销成功"
                msg["code"] = "200"
                msg["type"] = "success"

            else:
                msg["message"] = "用户身份非法"
                msg["code"] = "500"
                msg["type"] = "error"
        else:
            msg["message"] = "用户身份过期，请重新登录"
            msg["code"] = "300"
            msg["type"] = "error"
    else:
        msg["message"] = "用户不存在"
        msg["code"] = "500"
        msg["type"] = "error"

    return msg

@bp.route("/upUserCollectionJob", methods=["POST"])
def upUserCollectionJob():
    msg = {
        "message": "",
        "type": "",
        "code": "",
    }
    # 检查身份
    identity = request.json["identity"]
    loginId = request.json["loginId"]

    if loginId:
        res = redisDB.handle_redis_token(loginId)
        if res:
            if identity == "user":
                jobId = request.json["jobId"]
                userAndJobModel = UserAndJobModel.query.filter_by(uId=loginId, jobId=jobId).first()
                if userAndJobModel:
                    if userAndJobModel.isCollection == "true" and userAndJobModel.isApply == "true":
                        userAndJobModel.isCollection = "false"
                        db.session.commit()
                        msg["message"] = "取消收藏成功"
                        msg["code"] = "200"
                        msg["type"] = "success"


                    elif userAndJobModel.isCollection == "true" and userAndJobModel.isApply in ["false",None]:
                        db.session.delete(userAndJobModel)
                        db.session.commit()
                        msg["message"] = "取消收藏成功"
                        msg["code"] = "200"
                        msg["type"] = "success"
                    else:
                        msg["message"] = "未知错误"
                        msg["code"] = "500"
                        msg["type"] = "error"
                else:
                    msg["message"] = "用户未收藏和申请任何岗位"
                    msg["code"] = "500"
                    msg["type"] = "error"

            else:
                msg["message"] = "用户身份非法"
                msg["code"] = "500"
                msg["type"] = "error"
        else:
            msg["message"] = "用户身份过期，请重新登录"
            msg["code"] = "300"
            msg["type"] = "error"
    else:
        msg["message"] = "用户不存在"
        msg["code"] = "500"
        msg["type"] = "error"

    return msg


# 获取用户收藏岗位的详情
@bp.route("/getUserCollectionJobs", methods=["POST"])
def getUserCollectionJobs():
    msg = {
        "message": "",
        "type": "",
        "code": "",
        "datas": [],
        "industrys": [],
        "es": [],
    }
    # 检查身份
    identity = request.json["identity"]
    loginId = request.json["loginId"]
    token = request.json["token"]
    # 检查身份是否过期
    if loginId:
        res = redisDB.handle_redis_token(loginId)
        if res:
            if identity == "user":
                userAndJobModel = UserAndJobModel.query.filter_by(uId=loginId).all()
                job_lis = []
                e_lis = []
                if userAndJobModel:
                    for i in userAndJobModel:
                        if i.isCollection == "true":
                            job = JobModel.query.filter_by(jobId=i.jobId).first()
                            if job:
                                msg["datas"].append(
                                    {
                                        "jobId": i.jobId,
                                        "applyState": i.applyState,
                                        "jobName": job.jobName,
                                        "jobIndustry": job.jobIndustry.split("-")[1],
                                        "eName": job.hr_list[0].e_hr.businessName,
                                        "eId": job.hr_list[0].e_hr.eId,
                                    }
                                )
                                job_lis.append(
                                    {
                                        "text": job.jobIndustry.split("-")[1],
                                        "value": job.jobIndustry.split("-")[1],
                                    }
                                )
                                e_lis.append(
                                    {
                                        "text": job.hr_list[0].e_hr.businessName,
                                        "value": job.hr_list[0].e_hr.businessName,
                                    }
                                )
                    for i in job_lis:
                        if i not in msg["industrys"]:
                            msg["industrys"].append(i)
                    for i in e_lis:
                        if i not in msg["es"]:
                            msg["es"].append(i)
                    msg["message"] = "获取成功"
                    msg["type"] = "success"
                    msg["code"] = "200"
                else:
                    msg["message"] = "未申请岗位"
                    msg["type"] = "warning"
                    msg["code"] = "201"
            else:
                msg["message"] = "用户身份非法"
                msg["type"] = "error"
                msg["code"] = "500"
        else:
            msg["message"] = "用户身份过期，请重新登录"
            msg["code"] = "300"
            msg["type"] = "error"
    else:
        msg["message"] = "用户不存在"
        msg["code"] = "500"
        msg["type"] = "error"

    return msg


# 获取申请岗位的详情
@bp.route("/getApplyJobResult", methods=["POST"])
def getApplyJobResult():
    msg = {
        "message": "",
        "type": "",
        "code": "",
        "stateContent": "",

    }
    # 检查身份
    identity = request.json["identity"]
    loginId = request.json["loginId"]
    token = request.json["token"]
    # 检查身份是否过期
    if loginId:
        res = redisDB.handle_redis_token(loginId)
        if res:
            if identity == "user":
                jobId=request.json["jobId"]
                userAndJobModel = UserAndJobModel.query.filter_by(uId=loginId,jobId=jobId).first()

                if userAndJobModel:

                    msg["stateContent"] = userAndJobModel.stateContent,

                    msg["message"] = "获取成功"
                    msg["type"] = "success"
                    msg["code"] = "200"
                else:
                    msg["message"] = "未申请岗位"
                    msg["type"] = "warning"
                    msg["code"] = "201"
            else:
                msg["message"] = "非法用户"
                msg["type"] = "error"
                msg["code"] = "501"
        else:
            msg["message"] = "用户身份过期，请重新登录"
            msg["code"] = "300"
            msg["type"] = "error"
    return msg

# 获取用户申请的岗位
@bp.route("/getUserApplyJobs", methods=["POST"])
def getUserApplyJobs():
    msg = {
        "message": "",
        "type": "",
        "code": "",
        "datas": [],
        "industrys": [],
        "es": [],
        "applyjobstate": [],
    }
    # 检查身份
    identity = request.json["identity"]
    loginId = request.json["loginId"]
    token = request.json["token"]
    # 检查身份是否过期
    if loginId:
        res = redisDB.handle_redis_token(loginId)
        if res:
            if identity == "user":
                userAndJobModel = UserAndJobModel.query.filter_by(uId=loginId).all()
                job_lis = []
                e_lis=[]
                state=[]
                if userAndJobModel:
                    for i in userAndJobModel:
                        if i.isApply == "true":
                            job = JobModel.query.filter_by(jobId=i.jobId).first()
                            if job:
                                msg["datas"].append(
                                    {
                                        "jobId": i.jobId,
                                        "applyState": i.applyState,
                                        # "stateContent": i.stateContent,
                                        "jobName": job.jobName,
                                        "jobIndustry":job.jobIndustry.split("-")[1],
                                        "eName": job.hr_list[0].e_hr.businessName,
                                        "eId": job.hr_list[0].e_hr.eId,
                                    }
                                )

                                job_lis.append(
                                    {
                                        "text":job.jobIndustry.split("-")[1],
                                        "value":job.jobIndustry.split("-")[1],
                                    }
                                )
                                e_lis.append(
                                    {
                                        "text": job.hr_list[0].e_hr.businessName,
                                        "value": job.hr_list[0].e_hr.businessName,
                                    }
                                )
                                state.append(
                                    {
                                        "text":i.applyState,
                                        "value": i.applyState,
                                    }
                                )

                    for i in job_lis:
                        if i not in msg["industrys"]:
                            msg["industrys"].append(i)
                    for i in e_lis:
                        if i not in msg["es"]:
                            msg["es"].append(i)
                    for i in state:
                        if i not in msg["applyjobstate"]:
                            msg["applyjobstate"].append(i)
                    msg["message"] = "获取成功"
                    msg["type"] = "success"
                    msg["code"] = "200"
                else:
                    msg["message"] = "未申请岗位"
                    msg["type"] = "warning"
                    msg["code"] = "201"
            else:
                msg["message"] = "非法用户"
                msg["type"] = "error"
                msg["code"] = "501"
        else:
            msg["message"] = "用户身份过期，请重新登录"
            msg["code"] = "300"
            msg["type"] = "error"
    return msg


# 修改用户邮箱
@bp.route("/upUserEmail", methods=["POST"])
def upUserEmail():
    msg = {
        "message": "",
        "type": "",
        "code": ""
    }
    identity = request.json["identity"]
    if identity == "user":
        uId = request.json["loginId"]
        if uId:
            userModel = UserModel.query.filter_by(uId=uId).first()

            if userModel:
                email=request.json["email"]
                u1= UserModel.query.filter_by(email=email).first()
                # 判断是否存在相同的邮箱
                if u1:
                    # 判断是否本人修改相同的邮箱，是则允许修改
                    if u1.uId==userModel.uId:
                        email = email
                        userModel.email = email
                        db.session.commit()
                        msg["message"] = "邮箱修改成功"
                        msg["type"] = "success"
                        msg["code"] = "200"
                    else:
                        # 不是本人的邮箱，但又存在，则不修改
                        msg["message"] = "此邮箱不可使用，请更换"
                        msg["type"] = "error"
                        msg["code"] = "301"
                else:
                    # 不存在该邮箱，则可修改
                    email = email
                    userModel.email = email
                    db.session.commit()
                    msg["message"] = "邮箱修改成功"
                    msg["type"] = "success"
                    msg["code"] = "200"

            else:
                msg["message"] = "非法用户"
                msg["type"] = "error"
                msg["code"] = "500"
        else:
            msg["message"] = "非法用户"
            msg["type"] = "error"
            msg["code"] = "500"
    else:
        msg["message"] = "非法用户"
        msg["type"] = "error"
        msg["code"] = "500"
    return msg

# 求职者更改联系电话
@bp.route("/upUserPhone", methods=["POST"])
def upUserPhone():
    msg = {
        "message": "",
        "type": "",
        "code": ""
    }
    identity = request.json["identity"]
    if identity == "user":
        uId = request.json["loginId"]
        if uId:
            userModel = UserModel.query.filter_by(uId=uId).first()
            phone=request.json["phone"]
            if userModel:
                # 查询是否有重复的电话
                u1=UserModel.query.filter_by(phone=phone).first()
                # 有重复电话，则判断是不是本人的
                if u1:
                    # 本人修改了相同的电话，允许修改
                    if u1.uId==userModel.uId:
                        phone = phone
                        userModel.phone = phone
                        db.session.commit()
                        msg["message"] = "修改成功"
                        msg["type"] = "success"
                        msg["code"] = "200"
                    else:
                        # 不是本人的电话，且存在，则重复，不给更改
                        msg["message"] = "此联系方式不可使用，请更换"
                        msg["type"] = "error"
                        msg["code"] = "301"
                else:
                    # 不是本人，且不存在
                    phone = phone
                    userModel.phone = phone
                    db.session.commit()
                    msg["message"] = "修改成功"
                    msg["type"] = "success"
                    msg["code"] = "200"

            else:
                msg["message"] = "非法用户"
                msg["type"] = "error"
                msg["code"] = "500"
        else:
            msg["message"] = "非法用户"
            msg["type"] = "error"
            msg["code"] = "500"
    else:
        msg["message"] = "非法用户"
        msg["type"] = "error"
        msg["code"] = "500"
    return msg

# 求职者更改密码
@bp.route("/upUserPwd", methods=["POST"])
def upUserPwd():
    msg = {
        "message": "",
        "type": "",
        "code": ""
    }
    identity = request.json["identity"]
    if identity == "user":
        uId = request.json["loginId"]
        if uId:
            userModel = UserModel.query.filter_by(uId=uId).first()
            if userModel:
                password = request.json["password"]

                userModel.password = generate_password_hash(password)
                db.session.commit()
                msg["message"] = "密码修改成功，请重新登录"
                msg["type"] = "success"
                msg["code"] = "200"

            else:
                msg["message"] = "非法用户"
                msg["type"] = "error"
                msg["code"] = "500"
        else:
            msg["message"] = "非法用户"
            msg["type"] = "error"
            msg["code"] = "500"
    else:
        msg["message"] = "非法用户"
        msg["type"] = "error"
        msg["code"] = "501"
    return msg

# 校验求职者密码
@bp.route("/vUserPwd", methods=["POST"])
def vUserPwd():
    msg = {
        "message": "",
        "type": "",
        "code": ""
    }
    identity = request.json["identity"]
    if identity == "user":
        uId = request.json["loginId"]
        if uId:
            userModel = UserModel.query.filter_by(uId=uId).first()
            if userModel:
                password = request.json["password"]
                if check_password_hash(userModel.password, password):
                    msg["message"] = "密码正确"
                    msg["type"] = "success"
                    msg["code"] = "200"
                else:
                    msg["message"] = "密码错误"
                    msg["type"] = "error"
                    msg["code"] = "301"

            else:
                msg["message"] = "用户不存在"
                msg["type"] = "error"
                msg["code"] = "500"
        else:
            msg["message"] = "非法用户"
            msg["type"] = "error"
            msg["code"] = "500"
    else:
        msg["message"] = "非法用户"
        msg["type"] = "error"
        msg["code"] = "501"
    return msg


# 获取特定岗位的详情
@bp.route("/getOneJobInfo", methods=["POST"])
def getOneJobInfo():
    msg = {
        "message": "",
        "type": "",
        "code": "",
    }
    identity = request.json["identity"]
    loginId = request.json["loginId"]
    token = request.json["token"]
    jobId = request.json["jobId"]
    # 只有用户才可知道自己的申请和收藏
    # 企业 管理员 游客不可申请和收藏
    if identity == "user":
        # 获取用户对这个岗位的申请和收藏信息
        userAndJobModel = UserAndJobModel.query.filter_by(uId=loginId, jobId=jobId).first()
        if userAndJobModel:
            # 存在申请或收藏记录
            msg["isApply"] = userAndJobModel.isApply
            msg["isCollection"] = userAndJobModel.isCollection
        else:
            # 不存在
            msg["isApply"] = "false"
            msg["isCollection"] = "false"

        # 获取这个岗位的信息

    jobModel = JobModel().query.filter_by(jobId=jobId).first()
    if jobModel:

        msg["jobId"] = jobModel.jobId
        msg["jobName"] = jobModel.jobName
        msg["jobAge"] = jobModel.jobAge
        msg["jobEdu"] = jobModel.jobEdu
        msg["jobEx"] = jobModel.jobEx
        msg["jobAddress"] = jobModel.jobAddress
        msg["jobSalary"] = jobModel.jobSalary
        msg["jobIndustry"] = jobModel.jobIndustry
        msg["jobDetail"] = jobModel.jobDetail
        msg["message"] = "获取成功"
        msg["type"] = "success"
        msg["code"] = "200"

        # 获取岗位对应公司信息
        # 岗位id唯一,且企业和岗位是一对多关系
        e = jobModel.hr_list[0].e_hr
        if e:
            # 若存在这个企业，一般情况下都存在
            msg["eId"] = e.eId
            msg["eName"] = e.businessName
            msg["eDetail"] = e.content
            msg["contactName"] = e.contactName
            msg["contactPhone"] = e.contactPhone
            msg["contactAddress"] = e.contactAddress
            msg["contactEmail"] = e.contactEmail

            msg["message"] = "获取成功"
            msg["type"] = "success"
            msg["code"] = "200"
        else:
            msg["message"] = "没有企业信息"
            msg["type"] = "error"
            msg["code"] = "500"

    else:
        msg["message"] = "没有该岗位信息"
        msg["type"] = "error"
        msg["code"] = "500"

    return msg

# 申请岗位操作
@bp.route("/applyJob", methods=["POST"])
def applyJob():
    msg = {
        "message": "",
        "type": "",
        "code": ""
    }
    identity = request.json["identity"]

    if identity == "user":
        uId = request.json["loginId"]
        jobId = request.json["jobId"]
        userExist = UserAndJobModel().query.filter_by(jobId=jobId, uId=uId).first()
        # 若存在，则可能是收藏并未申请
        if userExist:
            # 存在记录
            if userExist.isApply == "true":
                msg["message"] = "已申请"
                msg["type"] = "warning"
                msg["code"] = "500"
            else:
                isApply = request.json["isApply"]
                applyState = request.json["applyState"]
                # userAndJobModel = UserAndJobModel()
                # userExist.uId = uId
                # userExist.jobId = jobId
                userExist.isApply = isApply
                userExist.applyState = applyState
                # db.session.add(userAndJobModel)
                db.session.commit()
                msg["message"] = "申请成功"
                msg["type"] = "success"
                msg["code"] = "200"
        else:
            # 没有记录
            isApply = request.json["isApply"]
            applyState = request.json["applyState"]
            userAndJobModel = UserAndJobModel()
            userAndJobModel.uId = uId
            userAndJobModel.jobId = jobId
            userAndJobModel.isApply = isApply
            userAndJobModel.applyState = applyState
            db.session.add(userAndJobModel)
            db.session.commit()
            msg["message"] = "申请成功"
            msg["type"] = "success"
            msg["code"] = "200"
    else:
        msg["message"] = "请先登录，再申请岗位"
        msg["type"] = "warning"
        msg["code"] = "501"
    return msg

# 收藏岗位操作
@bp.route("/upCollectionJob", methods=["POST"])
def upCollectionJob():
    msg = {
        "message": "",
        "type": "",
        "code": "",
    }
    identity = request.json["identity"]

    if identity == "user":
        # 判断是否有记录
        uId = request.json["loginId"]
        jobId = request.json["jobId"]
        userExist = UserAndJobModel().query.filter_by(jobId=jobId, uId=uId).first()
        if userExist:
            # 存在记录
            isCollection = request.json["isCollection"]
            if isCollection == "true":
                # userAndJobModel = UserAndJobModel()
                # userExist.jobId = jobId
                # userExist.uId = uId
                userExist.isCollection = isCollection
                # db.session.add(userAndJobModel)
                db.session.commit()
                msg["code"]="200"
                msg["message"] = "收藏成功"
                msg["type"] = "success"

            elif isCollection == "false":
                # userAndJobModel = UserAndJobModel()
                # userExist.jobId = jobId
                # userExist.uId = uId
                userExist.isCollection = isCollection
                # db.session.add(userAndJobModel)
                db.session.commit()
                msg["code"] = "200"
                msg["message"] = "已取消收藏"
                msg["type"] = "success"
            else:
                msg["message"] = "操作失败"
                msg["type"] = "error"
        else:
            # 没有记录
            isCollection = request.json["isCollection"]
            userAndJobModel = UserAndJobModel()
            userAndJobModel.jobId = jobId
            userAndJobModel.uId = uId
            userAndJobModel.isCollection = isCollection
            db.session.add(userAndJobModel)
            db.session.commit()
            msg["code"] = "200"
            msg["message"] = "收藏成功"
            msg["type"] = "success"
    else:
        msg["message"] = "请先登录，再收藏岗位"
        msg["type"] = "warning"
        msg["code"] = "501"
    return msg


# 修改用户基本数据
@bp.route("/upUserInfo", methods=["POST"])
def upUserInfo():
    msg = {
        "message": "",
        "type": "",
        "code": ""
    }

    # 检查身份
    identity = request.json["identity"]
    upPart = request.json["upPart"]
    loginId = request.json["loginId"]
    if loginId:
        res = redisDB.handle_redis_token(loginId)
        if res:
            # 根据需要更新的部分，更新信息
            if identity == "user":
                userbasicinfo = UserModel.query.filter_by(uId=loginId).first()
                user = UserResumeModel.query.filter_by(uId=loginId).first()
                if userbasicinfo:
                    # 更新基本信息

                    if upPart == "basicInfo":
                        userbasicinfo.name = request.json["name"]
                        userbasicinfo.gander = request.json["gander"]
                        userbasicinfo.birth = request.json["birth"]
                        userbasicinfo.birthAddress = request.json["birthAddress"][0] + "-" + \
                                                     request.json["birthAddress"][1]
                        userbasicinfo.newAddress = request.json["newAddress"][0] + "-" + request.json["newAddress"][1]
                        userbasicinfo.politicalOutlook = request.json["politicalOutlook"]

                        # 数据自动提交，很疑惑
                        db.session.commit()
                        msg["message"] = "更新成功"
                        msg["type"] = "success"
                        msg["code"] = "200"
                    if user:
                        # 更新求职状态
                        if upPart == "jobState":
                            user.jobState = request.json["userJobState"]
                            # 数据自动提交，很疑惑
                            db.session.commit()
                            msg["message"] = "更新成功"
                            msg["type"] = "success"
                            msg["code"] = "200"

                        # 更新求职意向
                        elif upPart == "jobIden":
                            # print(request.json["jobIndustry"])
                            user.jobTitle = request.json["title"]
                            user.jobSalary = request.json["salaryMin"] + "-" + request.json["salaryMax"]
                            user.jobCity = request.json["city"][0] + "-" + request.json["city"][1]
                            user.jobNature = request.json["jobNature"]
                            user.jobIndustry = request.json["industry"][0] + "-" + request.json["industry"][1]
                            # 数据自动提交，很疑惑
                            db.session.commit()
                            msg["message"] = "更新成功"
                            msg["type"] = "success"
                            msg["code"] = "200"
                        # 更新教育经历
                        elif upPart == "eduInfo":

                            user.eduSchoolName = request.json["schoolName"]

                            user.eduEducation = request.json["education"]
                            user.eduMajor = request.json["major"]
                            user.eduMajorDetail = request.json["eduMajorDetail"]
                            user.schoolTime = request.json["schoolTime"][0] + "-" + request.json["schoolTime"][1]
                            db.session.commit()
                            # 数据自动提交，很疑惑

                            msg["message"] = "更新成功"
                            msg["type"] = "success"
                            msg["code"] = "200"
                        # 更新工作经历
                        elif upPart == "jobEx":
                            user.jobExTime = "-".join(request.json["jobExTime"])
                            user.jobExEnterprise = request.json["jobExEnterprise"]
                            user.jobExLevel = request.json["jobExLevel"]
                            user.jobExDetail = request.json["jobExDetail"]
                            db.session.commit()
                            msg["message"] = "更新成功"
                            msg["type"] = "success"
                            msg["code"] = "200"
                        # 更新项目经历
                        elif upPart == "projectEx":
                            user.projectTime = "-".join(request.json["projectTime"])
                            user.projectEnterprise = request.json["projectEnterprise"]
                            user.projectName = request.json["projectName"]
                            user.projectDetail = request.json["projectDetail"]
                            db.session.commit()
                            msg["message"] = "更新成功"
                            msg["type"] = "success"
                            msg["code"] = "200"
                        # 更新荣誉奖项
                        elif upPart == "honors":
                            user.userHonors = "-".join(request.json["userHonors"])
                            db.session.commit()
                            msg["message"] = "更新成功"
                            msg["type"] = "success"
                            msg["code"] = "200"
                        # 更新自我评价
                        elif upPart == "selfEvaluation":
                            user.userSelfEvaluation = request.json["userSelfEvaluation"]
                            db.session.commit()
                            msg["message"] = "更新成功"
                            msg["type"] = "success"
                            msg["code"] = "200"
                    else:
                        # 更新求职状态
                        if upPart == "jobState":
                            # model=UserResumeModel()
                            newUserResume = UserResumeModel(jobState=request.json["userJobState"], uId=loginId)
                            # model.jobState=newUserResume.jobState
                            db.session.add(newUserResume)
                            db.session.commit()
                            msg["message"] = "更新成功"
                            msg["type"] = "success"
                            msg["code"] = "200"

                        # 更新求职意向
                        elif upPart == "jobIden":
                            newUserResume = UserResumeModel(
                                jobTitle=request.json["title"],
                                jobSalary=request.json["salaryMin"] + "-" + request.json["salaryMax"],
                                jobCity=request.json["city"][0] + "-" + request.json["city"][1],
                                jobNature=request.json["jobNature"],
                                jobIndustry=request.json["industry"],
                                uId=loginId,
                            )
                            db.session.add(newUserResume)
                            db.session.commit()
                            msg["message"] = "更新成功"
                            msg["type"] = "success"
                            msg["code"] = "200"
                        # 更新教育经历
                        elif upPart == "eduInfo":
                            newUserResume = UserResumeModel(
                                eduSchoolName=request.json["schoolName"],
                                eduEducation=request.json["education"],
                                eduMajor=request.json["major"],
                                eduMajorDetail=request.json["eduMajorDetail"],
                                schoolTime=request.json["schoolTime"][0] + "-" + request.json["schoolTime"][1],
                                uId=loginId,
                            )
                            db.session.add(newUserResume)
                            db.session.commit()
                            msg["message"] = "更新成功"
                            msg["type"] = "success"
                            msg["code"] = "200"
                        # 更新工作经历
                        elif upPart == "jobEx":
                            newUserResume = UserResumeModel(
                                jobExTime="-".join(request.json["jobExTime"]),
                                jobExEnterprise=request.json["jobExEnterprise"],
                                jobExLevel=request.json["jobExLevel"],
                                jobExDetail=request.json["jobExDetail"],
                                uId=loginId,
                            )
                            db.session.add(newUserResume)
                            db.session.commit()
                            msg["message"] = "更新成功"
                            msg["type"] = "success"
                            msg["code"] = "200"
                        # 更新项目经历
                        elif upPart == "projectEx":
                            newUserResume = UserResumeModel(
                                projectTime="-".join(request.json["projectTime"]),
                                projectEnterprise=request.json["projectEnterprise"],
                                projectName=request.json["projectName"],
                                projectDetail=request.json["projectDetail"],
                                uId=loginId,
                            )
                            db.session.add(newUserResume)
                            db.session.commit()
                            msg["message"] = "更新成功"
                            msg["type"] = "success"
                            msg["code"] = "200"
                        # 更新荣誉奖项
                        elif upPart == "honors":
                            newUserResume = UserResumeModel(
                                userHonors="-".join(request.json["userHonors"]),
                                uId=loginId,
                            )
                            db.session.add(newUserResume)
                            db.session.commit()
                            msg["message"] = "更新成功"
                            msg["type"] = "success"
                            msg["code"] = "200"
                        # 更新自我评价
                        elif upPart == "selfEvaluation":
                            newUserResume = UserResumeModel(
                                userSelfEvaluation=request.json["userSelfEvaluation"],
                                uId=loginId,
                            )
                            db.session.add(newUserResume)
                            db.session.commit()
                            msg["message"] = "更新成功"
                            msg["type"] = "success"
                            msg["code"] = "200"
                else:
                    msg["message"] = "用户不存在"
                    msg["type"] = "error"
                    msg["code"] = "500"

            else:
                msg["message"] = "身份非法"
                msg["type"] = "error"
                msg["code"] = "500"
        else:
            msg["message"] = "用户身份过期，请重新登录"
            msg["code"] = "300"
            msg["type"] = "error"
    else:
        msg["message"] = "用户ID非法"
        msg["code"] = "500"
        msg["type"] = "error"

    return msg


# 获取用户基本数据
@bp.route("/getUserInfo", methods=["POST"])
def getUserInfo():
    msg = {
        "message": "",
        "type": "",
        "code": ""
    }

    # 检查身份
    identity = request.json["identity"]
    loginId = request.json["loginId"]
    if loginId:
        res = redisDB.handle_redis_token(loginId)
        if res:

            # 根据需要更新的部分，更新信息
            if identity == "user":
                user = UserModel.query.filter_by(uId=loginId).first()
                userResume = UserResumeModel.query.filter_by(uId=loginId).first()

                if user:
                    # 获取基本信息
                    msg["name"] = user.name
                    msg["gander"] = user.gander if user.gander else ""
                    if user.birth:
                        msg["birth"] = user.birth
                    else:
                        msg["birth"] = "-"

                    if user.birthAddress:
                        msg["birthAddress"] = user.birthAddress.split("-")
                    else:
                        msg["birthAddress"] = ["", ""]
                    if user.newAddress:
                        msg["newAddress"] = user.newAddress.split("-")
                    else:
                        msg["newAddress"] = ["", ""]

                    msg["politicalOutlook"] = user.politicalOutlook
                    msg["phone"] = user.phone
                    msg["email"] = user.email

                    if userResume:
                        # 获取求职状态
                        msg["jobState"] = userResume.jobState
                        # 获取求职意向
                        msg["jobTitle"] = userResume.jobTitle

                        msg["jobNature"] = userResume.jobNature
                        # msg["jobIndustry"] = userResume.jobIndustry

                        # 获取教育经历
                        msg["eduSchoolName"] = userResume.eduSchoolName
                        msg["eduEducation"] = userResume.eduEducation
                        msg["eduMajor"] = userResume.eduMajor
                        msg["eduMajorDetail"] = userResume.eduMajorDetail

                        # 工作经历

                        msg["jobExEnterprise"] = userResume.jobExEnterprise
                        msg["jobExLevel"] = userResume.jobExLevel
                        msg["jobExDetail"] = userResume.jobExDetail
                        # 项目经历

                        msg["projectEnterprise"] = userResume.projectEnterprise
                        msg["projectName"] = userResume.projectName
                        msg["projectDetail"] = userResume.projectDetail

                        # 自我评价
                        msg["userSelfEvaluation"] = userResume.userSelfEvaluation

                        if userResume.jobSalary:
                            msg["jobSalary"] = userResume.jobSalary.split("-")
                        else:
                            msg["jobSalary"] = ["", ""]

                        if userResume.jobCity:
                            msg["jobCity"] = userResume.jobCity.split("-")
                        else:
                            msg["jobCity"] = ["", ""]
                        if userResume.jobIndustry:
                            msg["jobIndustry"] = userResume.jobIndustry.split("-")
                        else:

                            msg["jobIndustry"] = ["",""]


                        if userResume.schoolTime:
                            msg["schoolTime"] = userResume.schoolTime.split("-")
                        else:
                            msg["schoolTime"] = ["", ""]
                        if userResume.jobExTime:
                            msg["jobExTime"] = userResume.jobExTime.split("-")
                        else:
                            msg["jobExTime"] = ["", ""]
                        if userResume.projectTime:
                            msg["projectTime"] = userResume.projectTime.split("-")
                        else:
                            msg["projectTime"] = ["", ""]
                        if userResume.userHonors:
                            # 荣誉奖项

                            msg["userHonors"] = userResume.userHonors.split("-")
                        else:

                            msg["userHonors"] = []

                        msg["message"] = "获取成功"
                        msg["type"] = "success"
                        msg["code"] = "200"
                    else:
                        # 获取求职状态
                        msg["jobState"] = ""
                        # 获取求职意向
                        msg["jobTitle"] = ""
                        msg["jobSalary"] = ["", ""]
                        msg["jobCity"] = ["", ""]
                        msg["jobNature"] = ""
                        msg["jobIndustry"] = ""

                        # 获取教育经历
                        msg["eduSchoolName"] = ""
                        msg["eduEducation"] = ""
                        msg["eduMajor"] = ""
                        msg["eduMajorDetail"] = ""
                        msg["schoolTime"] = ["", ""]
                        # 工作经历
                        msg["jobExTime"] = ["", ""]
                        msg["jobExEnterprise"] = ""
                        msg["jobExLevel"] = ""
                        msg["jobExDetail"] = ""
                        # 项目经历
                        msg["projectTime"] = ["", ""]
                        msg["projectEnterprise"] = ""
                        msg["projectName"] = ""
                        msg["projectDetail"] = ""
                        # 荣誉奖项
                        msg["userHonors"] = []
                        # 自我评价
                        msg["userSelfEvaluation"] = ""

                        msg["message"] = "获取成功"
                        msg["type"] = "success"
                        msg["code"] = "200"

                    # 数据自动提交，很疑惑
                    # db.session.commit()
                else:
                    msg["message"] = "用户不存在"
                    msg["type"] = "warning"
                    msg["code"] = "500"
            else:
                msg["message"] = "身份非法"
                msg["type"] = "error"
                msg["code"] = "500"
        else:
            msg["message"] = "用户身份过期，请重新登录"
            msg["code"] = "300"
            msg["type"] = "error"
    else:
        msg["message"] = "用户ID非法"
        msg["code"] = "500"
        msg["type"] = "error"

    return msg
